package _5thread;

import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
 * @Description:
 * @Author wangzhen
 * @Date 2022-06-18 16:58
 **/
class Teacher{
    volatile String name;
    volatile int age;

    public Teacher(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class AtomicFieldUpdaterTest {


    public static void main(String[] args) throws InterruptedException {

        Teacher teacher=new Teacher("张三",15);

        new Thread(()->{

            AtomicIntegerFieldUpdater ageUpdater = AtomicIntegerFieldUpdater.newUpdater(Teacher.class, "age");

            ageUpdater.set(teacher,16);

            System.out.println("修改结果为:"+ageUpdater.get(teacher));

        }).start();

        new Thread(()->{

            AtomicReferenceFieldUpdater nameUpdater = AtomicReferenceFieldUpdater.newUpdater(Teacher.class, String.class, "name");

            nameUpdater.set(teacher,"张三三");

            System.out.println("修改结果为:"+nameUpdater.get(teacher));

        }).start();



        Thread.sleep(1000);
        System.out.println(teacher);
    }
}
